package es.titoapps.spanishhandball.iu;

import es.titoapps.spanishhandball.R;
import es.titoapps.spanishhandball.configuration.Configuration;
import es.titoapps.spanishhandball.database.DatabaseUtilities;
import es.titoapps.spanishhandball.entities.New;
import es.titoapps.spanishhandball.tools.NetworkTools;
import es.titoapps.spanishhandball.tools.Utilities;

import android.app.Activity;
import android.app.ProgressDialog;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;


import android.text.method.ScrollingMovementMethod;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;



public class DetailedNewActivity extends Activity {

	// Progress Dialog
	private ProgressDialog pDialog;
	TextView textViewTitle,textViewContent,textViewHeader;
	ImageView imageView,imageViewShare,imageViewBack,imageViewBrowser;
	View lineView,lineViewHeader;
	New currentNew;
	ScrollView scrollViewNewFull;
	Handler handler;


	@Override
	public void onCreate(Bundle savedInstanceState) {
		this.requestWindowFeature(Window.FEATURE_NO_TITLE);
		//establecemos la vista que se corresponde con esta actividad
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.detailednewlayout);

		Intent i = getIntent();
		Bundle extras = i.getExtras();

		currentNew=extras.getParcelable("es.titoapps.spanishhandball.entities.new");
		
		RelativeLayout layoutmain=(RelativeLayout)findViewById(R.id.scrollViewMenuBarLayout);
		RelativeLayout layout=(RelativeLayout)layoutmain.findViewById(R.id.scrollViewMenuBarButtons);
		scrollViewNewFull=(ScrollView)findViewById(R.id.scrollViewFullNew);
		scrollViewNewFull.setSmoothScrollingEnabled(true);
		scrollViewNewFull.smoothScrollBy(0, 30);
		//			scrollViewNewFull.smoothScroll
		scrollViewNewFull.fling(0);
		lineViewHeader=(View)findViewById(R.id.lineViewHeader);
		textViewHeader=(TextView)layoutmain.findViewById(R.id.textViewHeader);
		imageViewShare=(ImageView)layout.findViewById(R.id.imageViewShare);
		imageViewBack=(ImageView)layout.findViewById(R.id.imageViewBack);
		imageViewBrowser=(ImageView)layout.findViewById(R.id.imageViewBrowser);
		imageViewShare.setOnClickListener(imageClick);
		imageViewBack.setOnClickListener(imageClick);
		imageViewBrowser.setOnClickListener(imageClick);
		
		LinearLayout layout2=(LinearLayout)findViewById(R.id.scrollViewLayout);
		textViewTitle= (TextView)layout2.findViewById(R.id.textViewTitle);
		textViewContent= (TextView)layout2.findViewById(R.id.textViewContent);
		textViewContent.setMovementMethod(new ScrollingMovementMethod());
		imageView=(ImageView)layout2.findViewById(R.id.imageView);
		
		
		lineView=(View)layout2.findViewById(R.id.lineView);
		
		// Loading News in Background Thread
		new LoadDetailedWebView().execute();

	}
	
	private OnClickListener imageClick=new OnClickListener() {
	    public void onClick(View v) {
	      
	    	if(v.equals(imageViewBack))
	    		onBackPressed();
	    	else if(v.equals(imageViewShare))
	    		Utilities.shareContent(currentNew,DetailedNewActivity.this);
	    	else if(v.equals(imageViewBrowser))
	    		Utilities.launchContentOnBrowser(currentNew,DetailedNewActivity.this);
	    		
	     }
	};
	
	/**
	 * Background Async Task to Load WebView
	 * */
	class LoadDetailedWebView extends AsyncTask<String, String, String> {

		/**
		 * Before starting background thread Show Progress Dialog
		 * */
		@Override
		protected void onPreExecute() {
			super.onPreExecute();
			pDialog = new ProgressDialog(DetailedNewActivity.this);
			pDialog.setMessage("Cargando Noticia ...");
			pDialog.setIndeterminate(false);
			pDialog.setCancelable(false);
			pDialog.show();
		}

		/**
		 * getting Inbox JSON
		 * */
		protected String doInBackground(String... args) {

			Intent intent=new Intent();
			
			if(currentNew.getContent()==null || (!currentNew.getImageLoaded() && Configuration.getConfiguration().shouldLoadImages())){
				
				String htmlData=NetworkTools.loadHTMLData(currentNew);
				
				if(htmlData!=null){
					currentNew.setContent(htmlData);
					DatabaseUtilities.updateContentAndImageOfSources(currentNew,DetailedNewActivity.this);
					intent.putExtra("content",currentNew.getContent());
					intent.putExtra("image", currentNew.getImage());
					setResult(CustomListFragment.CONTENT_LOADED, intent);
				}else{
				
					intent.putExtra("content","");
					setResult(CustomListFragment.CONTENT_ALREADY_LOADED, intent);
				}
			}
			return null;
		}

		
		/**
		 * After completing background task Dismiss the progress dialog
		 * **/
		protected void onPostExecute(String file_url) {
			// dismiss the dialog after getting all products

			runOnUiThread(new Runnable() {
				public void run() {
					
					if(currentNew.getContent()!=null){
						textViewHeader.setText(Utilities.getSourceName(currentNew, DetailedNewActivity.this).toUpperCase());
						textViewTitle.setText(currentNew.getTitle());
						textViewContent.setText(currentNew.getContent());
						lineView.setVisibility(View.VISIBLE);
						lineViewHeader.setVisibility(View.VISIBLE);
						byte[]bb=currentNew.getImage();

						if(Configuration.getConfiguration().shouldLoadImages() && bb!=null){
							imageView.setVisibility(View.VISIBLE);
							Bitmap bitmap=BitmapFactory.decodeByteArray(bb, 0, bb.length);
							imageView.setImageBitmap(bitmap);
							imageView.setOnClickListener(new View.OnClickListener() {

								@Override
								public void onClick(View view) {
									// do stuff	
								}

							});
						}else
							imageView.setVisibility(View.GONE);
					}else{
						Toast toast=Toast.makeText(DetailedNewActivity.this,getString(R.string.mensaje_noticia_no_existe) , Toast.LENGTH_SHORT);
						toast.show();
						onBackPressed();
					}

				}
			});
			
			pDialog.dismiss();
		}

	}

}
